//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cancel.asyncImage;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import java.io.File;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CLocalImageLoader {
    int REQUIRED_SIZE = 100;
    private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap());
    ExecutorService executorService = Executors.newFixedThreadPool(5);

    public CLocalImageLoader(Context context) {
    }

    public void setREQUIRED_SIZE(int rEQUIRED_SIZE) {
        this.REQUIRED_SIZE = rEQUIRED_SIZE;
    }

    public void DisplayImage(String url, ImageView imageView) {
        if(url != null && url.length() > 0) {
            this.imageViews.put(imageView, url);
            this.queuePhoto(url, imageView);
        }

    }

    private void queuePhoto(String url, ImageView imageView) {
        CLocalImageLoader.PhotoToLoad p = new CLocalImageLoader.PhotoToLoad(url, imageView);
        this.executorService.submit(new CLocalImageLoader.PhotosLoader(p));
    }

    private Bitmap getBitmap(String url) {
        try {
            Bitmap bitmap = null;
            File file = new File(url);
            if(file.exists()) {
                bitmap = this.decodeFileFromPath(url);
            }

            file = null;
            return bitmap;
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

    private Bitmap decodeFileFromPath(String path) {
        Options o = new Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, o);
        int width_tmp = o.outWidth;
        int height_tmp = o.outHeight;

        int scale;
        for(scale = 1; width_tmp / 2 >= this.REQUIRED_SIZE && height_tmp / 2 >= this.REQUIRED_SIZE; scale *= 2) {
            width_tmp /= 2;
            height_tmp /= 2;
        }

        Options o2 = new Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeFile(path, o2);
    }

    boolean imageViewReused(CLocalImageLoader.PhotoToLoad photoToLoad) {
        String tag = (String)this.imageViews.get(photoToLoad.imageView);
        return tag == null || !tag.equals(photoToLoad.url);
    }

    class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        CLocalImageLoader.PhotoToLoad photoToLoad;

        public BitmapDisplayer(Bitmap b, CLocalImageLoader.PhotoToLoad p) {
            this.bitmap = b;
            this.photoToLoad = p;
        }

        public void run() {
            if(!CLocalImageLoader.this.imageViewReused(this.photoToLoad)) {
                if(this.bitmap != null) {
                    this.photoToLoad.imageView.setBackgroundDrawable(new BitmapDrawable(this.bitmap));
                }

            }
        }
    }

    private class PhotoToLoad {
        public String url;
        public ImageView imageView;

        public PhotoToLoad(String u, ImageView i) {
            this.url = u;
            this.imageView = i;
        }
    }

    class PhotosLoader implements Runnable {
        CLocalImageLoader.PhotoToLoad photoToLoad;

        PhotosLoader(CLocalImageLoader.PhotoToLoad photoToLoad) {
            this.photoToLoad = photoToLoad;
        }

        public void run() {
            if(!CLocalImageLoader.this.imageViewReused(this.photoToLoad)) {
                Bitmap bmp = CLocalImageLoader.this.getBitmap(this.photoToLoad.url);
                if(bmp != null) {
                    if(!CLocalImageLoader.this.imageViewReused(this.photoToLoad)) {
                        CLocalImageLoader.BitmapDisplayer bd = CLocalImageLoader.this.new BitmapDisplayer(bmp, this.photoToLoad);
                        Activity a = (Activity)this.photoToLoad.imageView.getContext();
                        a.runOnUiThread(bd);
                    }
                }
            }
        }
    }
}
